<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <!-- <view class="jtop_l" @tap="navigateBack"><image src="../../static/img/ic_back.png"></image></view> -->
      <view class="jtop_l" @tap="navigateBack">取消</view>
      <view class="jtop_c">编辑</view>
      <view
        class="jtop_r"
        :style="{ opacity: name ? '1' : '.6' }"
        @click="jsave"
        >确定</view
      >
    </view>
    <view class="tophr"></view>

    <view class="ubs">
      <view class="ub">
        <view class="ub_l">姓名</view>
        <input
          type="text"
          placeholder="请输入姓名"
          :value="name"
          @input="onKeyInput_o"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="ub">
        <view class="ub_l">性别</view>
        <image src="../../static/img/user/ic_right.png" class="ub_jt"></image>

        <picker
          @change="bindPickerChange"
          :value="index"
          :range="jsex"
          class="ub_t"
        >
          <view class="uni-input">{{ dw }}</view>
        </picker>
      </view>
      <!-- <view class="ub uba"  @click="jshow_pop">
				<view class="ub_l">头像</view>
				
				<image src="../../static/img/user/ic_right.png" class="ub_jt"></image>
				
				<image :src="mrtx" v-if="!avatar" class="ub_tx"></image>
				<image :src="avatar" v-if="avatar" class="ub_tx"></image>
				
			</view> -->
    </view>

    <view class="ubs">
      <view class="ub">
        <view class="ub_l">电话</view>
        <input
          type="tel"
          placeholder="请输入电话"
          maxlength="11"
          :value="phone"
          @input="onKeyInput_t"
          placeholder-style="color:#CCCCCC"
        />
      </view>
      <view class="ub">
        <view class="ub_l">邮箱</view>
        <input
          type="text"
          placeholder="请输入邮箱(选填)"
          :value="email"
          @input="onKeyInput_c"
          placeholder-style="color:#CCCCCC"
        />
      </view>
    </view>

    <view class="ubs">
      <view class="ub">
        <view class="ub_l">公司</view>

        <input
          type="text"
          placeholder="请输入公司(选填)"
          :value="identity"
          @input="onKeyInput_f"
          placeholder-style="color:#CCCCCC"
        />

        <!-- <image src="../../static/img/user/ic_right.png" class="ub_jt"></image>
				<view class="ub_t">甲方</view> -->
      </view>
      <view class="ub">
        <view class="ub_l">职位</view>
        <input
          type="text"
          placeholder="请输入职位(选填)"
          :value="duty"
          @input="onKeyInput_i"
          placeholder-style="color:#CCCCCC"
        />
        <!-- <image src="../../static/img/user/ic_right.png" class="ub_jt"></image>
				<view class="ub_t">总经理</view> -->
      </view>
    </view>

    <!-- 弹出层 -->
    <view :class="[ashowpop ? 'tc' : 'tc hide']">
      <view class="jtop_bj" @click="jshow_pop"></view>

      <!-- 分享 -->
      <view class="tc_fbox">
        <view class="umt" @click="changeAvatar">
          拍照
        </view>
        <view class="umt noborder" @click="changeAvatar2">
          从相册中选取
        </view>
        <view class="tc_fbox_bt" @click="jshow_pop">
          取消
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },

  data() {
    return {
      repeat_click: true,
      show_xl: false,
      js: '',
      ky: true,
      id: '',
      jsid: '',
      items: [],
      dyid: '',
      mrtx: '../../static/img/Default_avatar.png',
      avatar: '',
      mrtx2: '',
      dw: '女',
      index: '0',
      jsex: ['女', '男', '其他'],
      jsex2: [
        { value: '0', label: '女' },
        { value: '1', label: '男' },
        { value: '2', label: '其他' },
      ],
      ashowpop: false,
      name: '',
      phone: '',
      email: '',
      identity: '',
      duty: '',
    };
  },

  onLoad() {
    this.jc = uni.getStorageSync('jc');
    console.log('ccccc:', this.jc);

    this.id = this.jc.id;
    this.name = this.jc.name;
    this.phone = this.jc.phone;
    this.gender = this.jc.gender;
    this.id = this.jc.id;
    this.projectId = this.jc.projectId;

    if (this.jc.email) {
      this.email = this.jc.email;
    }
    if (this.jc.identity) {
      this.identity = this.jc.identity;
    }
    if (this.jc.duty) {
      this.duty = this.jc.duty;
    }
    if (this.jc.avatar) {
      this.avatar = this.jc.avatar;
    }
    this.dw = this.jsex[this.gender];
    console.log(this.jsex[this.gender]);
  },
  methods: {
    onKeyInput_o: function(event) {
      this.name = event.detail.value;
    },
    onKeyInput_t: function(event) {
      this.phone = event.detail.value;
    },
    onKeyInput_c: function(event) {
      this.email = event.detail.value;
    },
    onKeyInput_f: function(event) {
      this.identity = event.detail.value;
    },
    onKeyInput_i: function(event) {
      this.duty = event.detail.value;
    },

    jshow_pop() {
      this.ashowpop = !this.ashowpop;
    },

    changeAvatar() {
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['camera'], //从相册选择、摄像头
        success: function(res) {
          console.log(JSON.stringify(res.tempFilePaths, res.tempFiles[0]));

          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },
    changeAvatar2() {
      var that = this;
      uni.chooseImage({
        count: 1, //默认9
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择、摄像头
        success: function(res) {
          console.log(JSON.stringify(res.tempFilePaths, res.tempFiles[0]));
          that.sctp(res.tempFilePaths, res.tempFiles[0]);
        },
      });
    },

    sctp(a, b) {
      console.log(a);
      var als = '';
      var tpbt = b.name;
      if (tpbt == undefined || tpbt == 'undefined') {
        tpbt = b.path;
      }
      var pos = tpbt.lastIndexOf('.');
      var suffix = tpbt.substring(pos);
      function random_string(len) {
        len = len || 32;
        var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
        var maxPos = chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
          pwd += chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
      }
      var g_object_name = random_string(10) + suffix;

      // 获取信息
      request({
        url: '/biz/oss/policy',
        method: 'GET',

        success: res => {
          if (res.data.success == true) {
            als = res.data.data;
            request({
              filePath: a[0],
              oss: true,
              formData: {
                name: tpbt,
                key: als.dir + g_object_name,
                policy: als.policy,
                OSSAccessKeyId: als.accessid,
                signature: als.signature,
                callback: als.callback,
                success_action_status: '200',
              },

              success: res => {
                var cc = JSON.parse(res.data);
                console.log('上传图片', cc);
                if (cc.code == '00000') {
                  // var dd = this.jimg;
                  // dd.push(cc.data.url+'/'+als.dir + g_object_name);
                  // this.jimg = dd;

                  this.ashowpop = false;
                  this.avatar = cc.data.url + '/' + als.dir + g_object_name;
                } else {
                  this.repeat_click = true;
                  uni.showToast({
                    title: cc.message,
                    icon: 'none',
                  });
                }
              },
            });
          }
        },
      });
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;
      if (!this.name) {
        this.repeat_click = true;
        uni.showToast({
          title: '请输入姓名',
          icon: 'none',
        });
        return;
      }

      var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(this.phone)) {
        this.repeat_click = true;
        uni.showToast({
          title: '手机号码有误，请重填',
          icon: 'none',
        });
        return;
      }
      // if(!this.phone){
      // 						this.repeat_click = true;
      //	uni.showToast({
      // 		title: '请输入电话',
      // 		icon: 'none'
      // 	})
      // 	return
      // }

      var jd = {
        // id:this.id,
        // projectAddrBookRequest:{}
      };

      jd.name = this.name;
      jd.phone = this.phone;
      jd.gender = this.index;

      if (this.email) {
        jd.email = this.email;
      }
      if (this.identity) {
        jd.identity = this.identity;
      }
      if (this.duty) {
        jd.duty = this.duty;
      }
      if (this.avatar) {
        jd.avatar = this.avatar;
      }

      request({
        url: `/biz/addr-book/${this.id}`,
        data: jd,

        method: 'PUT',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '修改成功',
              icon: 'none',
              duration: 1000,
              success: function() {
                setTimeout(function() {
                  uni.navigateTo({
                    url: '../main/tel',
                  });
                  // uni.navigateBack()
                }, 1500);
              },
            });

            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    bindPickerChange: function(e) {
      console.log('picker发送选择改变，携带值为', e.target.value);
      this.index = e.target.value;
      this.dw = this.jsex2[this.index].label;
    },

    ...mapMutations(['logout']),
    user_edit_identity() {
      uni.navigateTo({
        url: '../user/user_edit_identity',
      });
    },
    user_edit_mail() {
      uni.navigateTo({
        url: '../user/user_edit_mail',
      });
    },
    user_edit_phone() {
      uni.navigateTo({
        url: '../user/user_edit_phone',
      });
    },
    user_edit_sex() {
      uni.navigateTo({
        url: '../user/user_edit_sex',
      });
    },
    user_edit_name() {
      uni.navigateTo({
        url: '../user/user_edit_name',
      });
    },
    user_head() {
      uni.navigateTo({
        url: '../user/user_head',
      });
    },
    bindLogin() {
      uni.navigateTo({
        url: '../login/login',
      });
    },
    bindLogout() {
      this.logout();
      /**
       * 如果需要强制登录跳转回登录页面
       */
      if (this.forcedLogin) {
        uni.reLaunch({
          url: '../login/login',
        });
      }
    },
    navigateBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
.ubs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}
.ub {
  width: 720rpx;
  height: 90rpx;
  display: block;
  float: right;
  line-height: 90rpx;
  border-bottom: 1px solid #dddddd;
}
.ubs .ub:last-child {
  border: 0;
}
.ub_l {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 34rpx;
  line-height: 90rpx;
  color: #000000;
  margin: 0rpx 0 0 0;
}
.ub_jt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}
.ub_t {
  width: auto;
  height: 40rpx;
  display: block;
  float: right;
  font-size: 32rpx;
  line-height: 40rpx;
  text-align: right;
  color: #999999;
  margin: 25rpx 20rpx 0 0;
}
.ub_t image {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: right;
  margin: 0rpx 0rpx 0 6rpx;
}
.ub_tx {
  width: 114rpx;
  height: 114rpx;
  display: block;
  float: right;
  margin: 30rpx 20rpx 30rpx 0;
  border-radius: 8rpx;
}
.uba {
  height: auto;
}
.uba .ub_l {
  margin-top: 45rpx;
}
.uba .ub_jt {
  margin-top: 75rpx;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}

.jtop_lt {
  margin: 0 0 0 10rpx;
}
/* .jtop_r{width: 48rpx; height: 48rpx; display: block;float: right; margin: 20rpx 20rpx 0 0;} */
/* .jtop_rl{margin: 20rpx 30rpx 0 0;} */
/* .jtop_r image{width: 48rpx; height: 48rpx; display: block;float: left;} */
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}

input {
  width: auto;
  height: 50rpx;
  display: block;
  float: right;
  margin: 20rpx 30rpx 0 0;
  text-align: right;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #999999;
}
.tc {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
  display: block;
}

.jtop_bj {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 10000;
}

.tc_box {
  width: 540rpx;
  height: auto;
  background: #ffffff;
  border-radius: 14rpx;
  margin: 460rpx 105rpx 0;
  position: relative;
  z-index: 10001;
  overflow: hidden;
}

.tc_boxt {
  width: 100%;
  height: 44rpx;
  font-size: 36rpx;
  line-height: 44rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 40rpx 0 20rpx;
  color: #000000;
}

.tc_boxtc {
  width: 100%;
  height: 36rpx;
  font-size: 28rpx;
  line-height: 36rpx;
  display: block;
  float: left;
  text-align: center;
  margin: 0rpx 0 30rpx;
  color: #666666;
}

.tc_boxi {
  width: 460rpx;
  height: 88rpx;
  background: #ffffff;
  border-radius: 8rpx;
  border: 1px solid #dddddd;
  display: block;
  float: left;
  margin: 0rpx 40rpx 22rpx 40rpx;
}

.tc_boxi input {
  width: 90%;
  height: 48rpx;
  margin: 20rpx 5%;
  font-size: 32rpx;
  color: #000000;
}

.tc_f {
  width: 100%;
  height: 100rpx;
  display: block;
  float: left;
  border-top: 1px solid #dcdee3;
  margin: 20rpx 0 0;
}

.tc_fl {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  border-right: 1px solid #dcdee3;
}

.tc_fr {
  width: 269rpx;
  height: 100rpx;
  line-height: 100rpx;
  text-align: center;
  font-size: 36rpx;
  color: #256aff;
  display: block;
  float: left;
  opacity: 0.5;
}

.tc_fbox {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 10001;
}

.tc_fboxs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
}

.tc_fbox_ic {
  width: 33.3333%;
  display: block;
  float: left;
  text-align: center;
  font-size: 28rpx;
  color: #666666;
}

.tc_fbox_ic image {
  width: 72rpx;
  height: 72rpx;
  display: block;
  float: none;
  margin: 60rpx auto 24rpx;
}

.tc_fbox_ic text {
  width: 100%;
  height: auto;
  display: block;
  float: none;
  margin: 0 auto 64rpx;
}

.tc_fbox_bt {
  width: 100%;
  height: 90rpx;
  line-height: 90rpx;
  display: block;
  float: left;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-top: 20rpx solid #f2f2f2;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  background: #ffffff;
}

.tc_list_t {
  width: 100%;
  height: 80rpx;
  display: block;
  float: left;
  background: #ffffff;
  font-size: 24rpx;
  color: #999999;
  line-height: 80rpx;
  margin: 0rpx 0;
  border-bottom: 1px solid #dddddd;
  text-align: center;
}

.tc_list {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  border-bottom: 1px solid #dddddd;
}

.tc_list1 {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 36rpx;
  color: #000000;
  line-height: 44rpx;
  margin: 34rpx 28rpx 38rpx 30rpx;
}

.tc_listtwo {
  width: auto;
  height: auto;
  display: block;
  float: left;
  font-size: 28rpx;
  color: #999999;
  line-height: 36rpx;
  margin: 42rpx 0rpx 0rpx 0rpx;
}

.tc_list image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: right;
  margin: 36rpx 30rpx 0 0;
}
.umt {
  width: 100%;
  height: 90rpx;
  display: block;
  float: left;
  line-height: 90rpx;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
  border-bottom: 1px solid #dddddd;
}
input {
  color: #000000 !important;
}
</style>
